/*
 * AmazonItemSearch.java
 *
 * Sample code from Amazon API
 * 
 * Calling Amazon API to search for an item
 */

package com.amazon.advertising.api;

import ie.dit.comp.lukejia.fyp.model.Item;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AmazonItemSearch {
	private static final String AWS_ACCESS_KEY_ID = "AKIAIR52JIIMRNFJIRNQ";
	private static final String AWS_SECRET_KEY = "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";
	private static SignedRequestsHelper helper;

	/*
	 * Use one of the following end-points, according to the region you are
	 * interested in:
	 * 
	 * US: ecs.amazonaws.com CA: ecs.amazonaws.ca UK: ecs.amazonaws.co.uk DE:
	 * ecs.amazonaws.de FR: ecs.amazonaws.fr JP: ecs.amazonaws.jp
	 */
	private static final String ENDPOINT = "ecs.amazonaws.com";

	public static List<Item> searchItem(String query, String category) {

		try {
			helper = SignedRequestsHelper.getInstance(ENDPOINT,
					AWS_ACCESS_KEY_ID, AWS_SECRET_KEY);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

		String index = "Books";
		if (category.equals("movie"))
			index = "Video";

		Map<String, String> params = new HashMap<String, String>();
		params.put("Service", "AWSECommerceService");
		params.put("Version", "2010-09-01");
		params.put("Operation", "ItemSearch");
		params.put("Keywords", query);
		params.put("SearchIndex", index);// Book or Video
		params.put("AssociateTag", "tag");

		String requestUrl = helper.sign(params);
		System.out.println("Signed Request is \"" + requestUrl + "\"");

		List<Item> items = AmazonAPIParser.getItemInfo(requestUrl);
		System.out.println(items.toString());
		return items;
	}

}
